package com.xxx.modules.controller;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PSW
 * @since 2021-11-04
 */
@RestController
@RequestMapping("/common/image")
@Slf4j
public class CommonController {


    @Value("${document.path}")
    private String documentPath;

    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam(name = "file",required = true) MultipartFile file) {
        String fileName = "";
        if (!file.isEmpty()) {
            try {
                //图片命名
                fileName = file.getOriginalFilename();
                File newFile = new File(documentPath);
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }
                File imageFile = new File(newFile,fileName);
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(imageFile));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return  ResultUtil.error(-1,"异常");
            } catch (IOException e) {
                e.printStackTrace();
                log.info("上传底图接口/uploadBaseImage出现异常,异常信息如下====>{}",e.getMessage());
                return  ResultUtil.error(-1,"异常");
            }
        }
//        return  ResultUtil.success(1,"图片上传成功！","文件名称"+fileName+"==="+"文件路径==>/search/compare/"+  fileName);
        return  ResultUtil.success(1,"图片上传成功",fileName);
    }

    @PostMapping("/upload")
    public Result upCqltjtCustomer( @RequestParam("files") MultipartFile [] files,
                                        HttpServletRequest request) throws Exception {
//        ReslutData reslutData = new ReslutData();
        List<String> fileNameList = new ArrayList<>();
        for (MultipartFile file:files){
            String fileName = "";
            if (!file.isEmpty()) {
                try {
                    //图片命名
                    fileName = Objects.requireNonNull(file.getOriginalFilename()).replaceAll(".+\\.", System.currentTimeMillis()+".");
                    File newFile = new File(documentPath);
                    if (!newFile.exists()) {
                        newFile.mkdirs();
                    }
                    File imageFile = new File(newFile,fileName);
                    BufferedOutputStream out = new BufferedOutputStream(
                            new FileOutputStream(imageFile));
                    out.write(file.getBytes());
                    out.flush();
                    out.close();
                    fileNameList.add(fileName);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.info("上传底图接口/uploadBaseImage出现异常,异常信息如下====>{}",e.getMessage());
                }
            }
        }

        return ResultUtil.success(1,"正常",fileNameList);
    }

    public static void main(String[] args) {
        String imgName = "1.jpg";
        imgName = imgName.replaceAll(".+\\.", System.currentTimeMillis()+".");
        System.out.println(imgName);
    }
}

